package easier.fast.service.observer;

import android.content.Context;
import androidx.lifecycle.Lifecycle;
import easier.fast.service.data.CodeDataMsgException;
import easier.fast.service.data.CodeMsgData;

/**
 * @param <T> 相对于{@link LifeObserver}增加异常统一处理
 */
public class BaseLifeObserver< T > extends LifeObserver< T > {

    protected final Context mContext;

    public BaseLifeObserver( Context context, Lifecycle lifecycle ) {

        super( lifecycle );
        mContext = context;
    }

    @Override
    public void onError( Throwable e ) {

        super.onError( e );

        if ( e instanceof CodeDataMsgException ) {
            CodeMsgData codeMsgData = ((CodeDataMsgException) e).getCodeMsgData();
            if ( codeMsgData != null ) {
                onCodeMsgDataError( codeMsgData );
            }
        } else {
            onConnectError();
        }
    }

    /**
     * 业务上的异常{@link CodeMsgData#code}不是200造成的异常在此处处理
     */
    protected void onCodeMsgDataError( CodeMsgData codeMsgData ) {

    }

    /**
     * 没有获取响应的异常
     */
    protected void onConnectError() {

    }
}
